package cn.yotta.user.core.server.dao.impl;

import cn.yotta.user.core.server.constants.DeleteStatus;
import cn.yotta.user.core.server.dao.UserDao;
import cn.yotta.user.core.server.entity.User;
import cn.yotta.user.core.server.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
/**
 * @author w_etw
 * @date 2023/4/15 21:25
 */
@Repository
public class UserDaoImpl extends ServiceImpl<UserMapper, User> implements UserDao {

    @Override
    public List<User> findUserByIds(Long tenantId, Collection<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return this.lambdaQuery()
                .in(User::getId, ids)
                .eq(Objects.nonNull(tenantId), User::getTenantId, tenantId)
                .eq(User::getDeleted, DeleteStatus.NOT_DELETED.getValue())
                .list();
    }

    @Override
    public User findUserById(Long tenantId, Long id) {
        User user = null;
        if (Objects.nonNull(id)) {
            user = this.lambdaQuery()
                    .eq(User::getId, id)
                    .eq(Objects.nonNull(tenantId), User::getTenantId, tenantId)
                    .eq(User::getDeleted, DeleteStatus.NOT_DELETED.getValue())
                    .one();
        }
        return user;
    }

    @Override
    public User findByUsername(Long tenantId, String username) {
        User user = null;
        if (StringUtils.isNotEmpty(username)) {
            user = this.lambdaQuery()
                    .eq(Objects.nonNull(tenantId), User::getTenantId, tenantId)
                    .eq(User::getUsername, username)
                    .eq(User::getDeleted, DeleteStatus.NOT_DELETED.getValue())
                    .one();
        }
        return user;
    }



    @Override
    public Boolean freezeUser(Long tenantId, Long id, Long operatorId) {
        return this.lambdaUpdate()
                .eq(Objects.nonNull(tenantId), User::getTenantId, tenantId)
                .eq(User::getId, id)
                .set(User::getLocked, true)
                .update();
    }

    @Override
    public Boolean unFreeze(Long tenantId, Long id, Long operatorId) {
        return this.lambdaUpdate()
                .eq(Objects.nonNull(tenantId), User::getTenantId, tenantId)
                .eq(User::getId, id)
                .set(User::getLocked, false)
                .update();
    }

    @Override
    public Boolean enable(Long tenantId, Long id, Long operatorId) {
        return this.lambdaUpdate()
                .eq(Objects.nonNull(tenantId), User::getTenantId, tenantId)
                .eq(User::getId, id)
                .set(User::getEnabled, true)
                .update();
    }

    @Override
    public Boolean disable(Long tenantId, Long id, Long operatorId) {
        final boolean update = this.lambdaUpdate()
                .eq(Objects.nonNull(tenantId), User::getTenantId, tenantId)
                .eq(User::getId, id)
                .set(User::getEnabled, false).update();
        return update;
    }

    @Override
    public User findUserByMobile(Long tenantId, String mobile) {
        User user = null;
        if (StringUtils.isNotEmpty(mobile)) {
            user = this.lambdaQuery()
                    .eq(Objects.nonNull(tenantId), User::getTenantId, tenantId)
                    .eq(User::getMobile, mobile)
                    .one();
        }
        return user;
    }

    @Override
    public Page<User> page(Integer pageNo, Integer pageSize, User user) {
        // 构建mybatis的分页参数
        final Page<User> page = new Page<>(pageNo, pageSize);
        // 生成查询参数
        Wrapper<User> queryWrapper = genCommonQueryWrapper(user);
        // 查询
        return this.page(page, queryWrapper);
    }

    protected Wrapper<User> genCommonQueryWrapper(User user) {
        if (Objects.isNull(user)) {
            return this.lambdaQuery().orderByDesc(User::getId);
        }
        return this.lambdaQuery()
                .eq(Objects.nonNull(user.getId()), User::getId, user.getId())
                .eq(Objects.nonNull(user.getTenantId()), User::getTenantId, user.getTenantId())
                .eq(StringUtils.isNotEmpty(user.getUsername()), User::getUsername, user.getUsername())
                .eq(StringUtils.isNotEmpty(user.getMobile()), User::getMobile, user.getMobile())
                .eq(StringUtils.isNotEmpty(user.getEmail()), User::getEmail, user.getEmail())
                .eq(Objects.nonNull(user.getEnabled()), User::getEnabled, user.getEnabled())
                .eq(Objects.nonNull(user.getLocked()), User::getLocked, user.getLocked())
                .eq(User::getDeleted, DeleteStatus.NOT_DELETED.getValue())
                .orderByDesc(User::getId)
                .getWrapper();
    }
}
